<!--
/// 
/// MaDAson - Toolkit for Model Driven Software Development
///
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 
-->
<%@ Page Language="C#" %>
<%@ Import Namespace="Madason.MetaModel" %>
<%@ Import Namespace="Madason.MetaModel.DAO" %>
package <%= Class.Namespace.FullName %>;

import java.util.*;
import java.sql.*;
import java.util.Date;
import java.lang.reflect.Constructor;

/**
 * <%= Class.Documentation.Replace("\n", "\n	* ") %>
 */
public abstract class <%= Class.Name + "Access" %>
{   
    public static <%= Class.Name %>Access getInstance()
	{
		String implName = System.getProperty("madason.dao.impl");
		if (implName == null)
			return new <%= Class.Name %>AccessOracle();
		else
		{
			try
			{
				ClassLoader classLoader = (new <%= Class.Name %>()).getClass().getClassLoader(); 
				Class cls = classLoader.loadClass("<%= Class.ExplicitName %>Access" + implName);
				Constructor ctor = cls.getConstructor(new Class[] {});
				return (<%= Class.Name %>Access) ctor.newInstance(new Object[] {});
			}
			catch (Exception e)
			{
				e.printStackTrace();
				return null;
			}
		}
	}

<%  if (!Class.Readonly) { %>


    /**
     * insert new <%= Class.Name %>
     */
	public abstract void insert(Connection conn, <%= Class.Name %> bean) throws Exception;


    /**
     * delete <%= Class.Name%>
     */
    public abstract void delete(Connection conn, <%= PrimaryKey.Type.Name%> primaryKey) throws Exception;


    /**
     * update new <%= Class.Name%>
     */
	public abstract void update(Connection conn, <%= Class.Name%> bean) throws Exception;

<% } %>

<% foreach (PersistedAssociation assoc in Class.ComplexAssociations) { %>
	/**
	 * update n-n relationships '<%= assoc.RoleName %>' 
	 */
    public abstract void update<%= StringUtil.MakePascalCase(assoc.RoleName) %>ByIds(Connection conn, int <%= PrimaryKey.NameCamelCase %>, List<%= "<Integer>" %> <%= assoc.RoleName %>) throws Exception;


	/**
	 * update n-n relationships '<%= assoc.RoleName %>' 
	 */
    public abstract void update<%= StringUtil.MakePascalCase(assoc.RoleName) %>(Connection conn, int <%= PrimaryKey.NameCamelCase %>, List<%= "<" + assoc.TargetClass.ExplicitName  + ">"%> <%= assoc.RoleName %>) throws Exception;

<% } %>


<% foreach (Finder finder in Class.GetAndExpandFinder())
   { %>

	/**
	 *  <%= Class.Documentation.Replace("\n", "\n\t\t * ") %>
	 */
	public abstract <%= OperationReturnType(finder) %> <%= finder.Name %>(Connection conn<%= FinderParamSep(finder) + finder.ParameterString %>) throws Exception;

<% } %>

	
}

<script runat="server">
	private Entity @class;
	private bool useGenerics;

	public bool UseGenerics
	{
		get { return useGenerics; }
		set { useGenerics = value; }
	}
	
	
	public Entity Class
	{
		get { return @class; }
		set { @class = value; }
	}

	public Madason.MetaModel.Attribute PrimaryKey
	{
		get
		{
			return Class.PrimaryKey;
		}
	}	
	
	protected string OperationReturnType(Operation finder)
	{
		if (UseGenerics) 
			return (finder.ReturnType.ExplicitName == "List") ?
			   "List<" + Class.Name + ">" :
			   finder.ReturnType.Name;
		else
			return (finder.ReturnType.ExplicitName == "List") ?
			   "List" :
			   finder.ReturnType.Name; 			   
	}
	
	protected string FinderParamSep(Finder finder)	
	{
		return finder.Parameters.Count > 0 ? ", " : "";
	}
</script>
